package com.pride1952.myoptimusx3;

import com.pride1952.myoptimusx3.util.SystemUiHider;

import android.annotation.TargetApi;
import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.LinearLayout;

public class WelcomeActivity extends Activity {
    private static final int GOTO_MAIN_ACTIVITY = 0;
    private static final int GOTO_DELAY_MILLS = 400 + 1000;

    private Button mBtnSignIn;
    private Button mBtnSignUp;
    LinearLayout welcomeBottomBar;

    private boolean isLogin;
    private Handler mHandler = new Handler() {
        public void handleMessage(android.os.Message msg) {
            switch (msg.what) {
                case GOTO_MAIN_ACTIVITY: {
                    if (!isLogin) {
                        welcomeBottomBar.setVisibility(View.VISIBLE);

                        //todo 欢迎页视频
                    }
                    else {
                        Intent intent = new Intent();
                        intent.setClass(WelcomeActivity.this, MainActivity.class);
                        startActivity(intent);
                        overridePendingTransition(R.anim.translate_in, R.anim.translate_out);
                        finish();
                    }
                    break;
                }
                default:
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_welcome);

        SharedPreferences mPreferences=getSharedPreferences("isLogin",MODE_PRIVATE);
        isLogin=mPreferences.getBoolean("isLogin",false);
        initWidgets();
        mHandler.sendEmptyMessageDelayed(GOTO_MAIN_ACTIVITY, GOTO_DELAY_MILLS);
    }

    private void initWidgets(){
        welcomeBottomBar=(LinearLayout)findViewById(R.id.welcome_bottom_bar);
        mBtnSignIn=(Button)findViewById(R.id.btn_login_signin);
        mBtnSignUp=(Button)findViewById(R.id.btn_login_signup);
        mBtnSignIn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                LoginFragment loginFragment = new LoginFragment();
                FragmentManager fm = getFragmentManager();
                FragmentTransaction transaction = fm.beginTransaction();
                transaction.replace(R.id.welcome_body, loginFragment);
                transaction.commit();

                welcomeBottomBar.setVisibility(View.GONE);
            }
        });
    }

};
